<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>课程报名 - 家长端</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <style>
        * {
            box-sizing: border-box;
            margin: 0;
            padding: 0;
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
        }
        
        body {
            background: linear-gradient(135deg, #6a11cb 0%, #2575fc 100%);
            color: #333;
            line-height: 1.6;
            padding: 20px;
            min-height: 100vh;
            display: flex;
            justify-content: center;
            align-items: center;
        }
        
        .container {
            width: 100%;
            max-width: 600px;
            background: white;
            border-radius: 15px;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.15);
            overflow: hidden;
        }
        
        header {
            background: #4a6fc3;
            color: white;
            padding: 25px;
            text-align: center;
        }
        
        h1 {
            font-size: 2.2rem;
            margin-bottom: 10px;
        }
        
        .subtitle {
            font-size: 1.1rem;
            opacity: 0.9;
        }
        
        .content {
            padding: 30px;
        }
        
        .form-group {
            margin-bottom: 20px;
        }
        
        label {
            display: block;
            margin-bottom: 8px;
            font-weight: 600;
            color: #2c3e50;
        }
        
        input, select {
            width: 100%;
            padding: 12px 15px;
            border: 2px solid #ddd;
            border-radius: 6px;
            font-size: 1rem;
            transition: border-color 0.3s;
        }
        
        input:focus, select:focus {
            border-color: #4a6fc3;
            outline: none;
        }
        
        .btn {
            padding: 12px 20px;
            border: none;
            border-radius: 6px;
            font-size: 1rem;
            font-weight: 600;
            cursor: pointer;
            transition: all 0.3s;
            width: 100%;
        }
        
        .btn-primary {
            background: #4a6fc3;
            color: white;
        }
        
        .btn-primary:hover {
            background: #3a5ba0;
        }
        
        .message {
            padding: 15px;
            margin-top: 20px;
            border-radius: 8px;
            font-weight: 500;
            text-align: center;
        }
        
        .error {
            background-color: #ffebee;
            color: #ff4757;
        }
        
        .success {
            background-color: #e3f2fd;
            color: #3498db;
        }
        
        .course-info {
            margin-top: 20px;
            padding: 15px;
            background: #f0f5ff;
            border-radius: 8px;
            text-align: center;
        }
        
        .course-name {
            font-weight: 600;
            color: #4a6fc3;
        }
        
        .course-capacity {
            color: #7f8c8d;
            margin-top: 5px;
        }
        
        .back-link {
            display: block;
            text-align: center;
            margin-top: 20px;
            color: #4a6fc3;
            text-decoration: none;
        }
        
        .back-link:hover {
            text-decoration: underline;
        }
    </style>
</head>
<body>
    <div class="container">
        <header>
            <h1>课程报名</h1>
            <p class="subtitle">请填写学生信息并选择课程</p>
        </header>
        
        <div class="content">
            <div class="form-group">
                <label for="studentName">学生姓名</label>
                <input type="text" id="studentName" placeholder="输入学生姓名">
            </div>
            
            <div class="form-group">
                <label for="studentClass">所属班级</label>
                <input type="text" id="studentClass" placeholder="输入学生班级">
            </div>
            
            <div class="form-group">
                <label for="courseSelect">选择课程</label>
                <select id="courseSelect">
                    <option value="">-- 请选择课程 --</option>
                    <!-- 课程选项将由JavaScript动态生成 -->
                </select>
            </div>
            
            <button class="btn btn-primary" id="enrollBtn">报名</button>
            
            <div class="message" id="parentMessage" style="display: none;"></div>
            
            <a href="admin.html" class="back-link">返回管理页面</a>
        </div>
    </div>

    <script>
        // 从本地存储获取数据
        let courses = JSON.parse(localStorage.getItem('courses')) || [];
        let students = JSON.parse(localStorage.getItem('students')) || [];

        // 页面加载时初始化
        document.addEventListener('DOMContentLoaded', function() {
            renderCourseOptions();
        });

        // 渲染课程选项
        function renderCourseOptions() {
            const courseSelect = document.getElementById('courseSelect');
            // 清空现有选项，但保留第一个默认选项
            courseSelect.innerHTML = '<option value="">-- 请选择课程 --</option>';
            
            if (courses.length === 0) {
                courseSelect.innerHTML = '<option value="" disabled>暂无可用课程</option>';
                return;
            }
            
            courses.forEach(course => {
                const option = document.createElement('option');
                option.value = course.id;
                option.textContent = `${course.name} (${course.enrolled}/${course.capacity})`;
                // 如果课程已满，禁用该选项
                if (course.enrolled >= course.capacity) {
                    option.disabled = true;
                    option.textContent += ' - 已满';
                }
                courseSelect.appendChild(option);
            });
        }

        // 报名课程
        document.getElementById('enrollBtn').addEventListener('click', function() {
            const studentName = document.getElementById('studentName').value.trim();
            const studentClass = document.getElementById('studentClass').value.trim();
            const courseId = parseInt(document.getElementById('courseSelect').value);
            
            if (!studentName || !studentClass) {
                showMessage('parentMessage', '请填写完整的学生信息', 'error');
                return;
            }
            
            if (!courseId) {
                showMessage('parentMessage', '请选择课程', 'error');
                return;
            }
            
            // 检查学生是否已报名（根据姓名和班级）
            if (students.some(s => s.name === studentName && s.class === studentClass)) {
                showMessage('parentMessage', '该学生已报名过课程', 'error');
                return;
            }
            
            const course = courses.find(c => c.id === courseId);
            if (course.enrolled >= course.capacity) {
                showMessage('parentMessage', '该课程已满员', 'error');
                return;
            }
            
            // 增加课程报名人数
            course.enrolled++;
            
            // 添加学生记录
            const newStudent = {
                name: studentName,
                class: studentClass,
                course: course.name
            };
            
            students.push(newStudent);
            saveData();
            
            // 更新界面
            renderCourseOptions();
            
            // 清空表单
            document.getElementById('studentName').value = '';
            document.getElementById('studentClass').value = '';
            document.getElementById('courseSelect').value = '';
            
            showMessage('parentMessage', '报名成功！感谢您的参与。', 'success');
        });

        // 显示消息
        function showMessage(elementId, message, type) {
            const messageEl = document.getElementById(elementId);
            messageEl.textContent = message;
            messageEl.className = `message ${type}`;
            messageEl.style.display = 'block';
            
            // 5秒后隐藏消息
            setTimeout(() => {
                messageEl.style.display = 'none';
            }, 5000);
        }

        // 保存数据到本地存储
        function saveData() {
            localStorage.setItem('courses', JSON.stringify(courses));
            localStorage.setItem('students', JSON.stringify(students));
        }
    </script>
</body>
</html>